<template>
  <view class="container">
    <view class="page-header">
      <view class="header-title">人员注册</view>
    </view>
    
    <view class="form-container">
      <view class="form-group">
        <view class="form-label">人员编码</view>
        <view class="form-input">
          <input v-model="formData.code" placeholder="请输入人员编码" />
        </view>
      </view>
      
      <view class="form-group">
        <view class="form-label">姓名</view>
        <view class="form-input">
          <input v-model="formData.name" placeholder="请输入姓名" required />
        </view>
      </view>
      
      <view class="form-group">
        <view class="form-label">联系电话</view>
        <view class="form-input">
          <input v-model="formData.telePhone" placeholder="请输入联系电话" type="tel" />
        </view>
      </view>
      
      <view class="form-group">
        <view class="form-label">身份证号码</view>
        <view class="form-select" >
           <input v-model="formData.cardId" type="number" maxlength="18"  placeholder="输入身份证号"/>
        </view>
      </view>
    </view>
    
    <view class="form-actions">
      <button class="btn cancel" @click="resetForm">重置</button>
      <button class="btn submit" @click="submitForm">提交注册</button>
    </view>
  </view>
</template>

<script>
const CouldDB=uniCloud.importObject('getDb')
export default {
	name:"regUser",
  data() {
    return {
      formData: {
        code: '',
        name: '',
        telePhone: '',
		cardId:""
  
      }
    }
  },
  onLoad() {

  },
  methods: {

    // 重置表单
    resetForm() {
      this.formData = {
        code: '',
        name: '',
        telePhone: '',
        cardId:""
      };
  
    },
    
    // 提交表单
async submitForm() {
  // 定义字段中文名称映射
  const fieldLabels = {
        code: '编码',
        name: '姓名',
        telePhone: '电话',
		cardId:"身份证号码"
  };
  
  // 收集空字段
  const emptyFields = [];
  Object.entries(this.formData).forEach(([field, value]) => {
    // 仅验证需要必填的字段（示例中name和own为必填）
    if (!value || (typeof value === 'string' && value.trim() === '')) {
      emptyFields.push(fieldLabels[field] || field);
    }
  });
  
  // 验证电话格式
  if (this.formData.telePhone && !/^1[3-9]\d{9}$/.test(this.formData.telePhone)) {
    emptyFields.push('联系电话格式');
  }
  
  // 显示错误
  if (emptyFields.length > 0) {
    uni.showToast({
      title: `${emptyFields.join('、')}不正确`,
      icon: 'none'
    });
    return;
  }
	  
  try{
	  uni.showLoading({
	    title: '提交中...'
	  });
	  const res= await CouldDB.addDB('user',this.formData)
  	if(res){
		uni.showToast({
			title:'人员注册成功',
		})
	this.resetForm()
	}else{
	uni.showToast({
		title:'人员注册失败',
		icon:'error'
		
	})
	}
		  
  }catch(e){
	  uni.showToast({
	  	title:'人员注册失败',
	  	icon:'error'
	  	
	  })
	  console.error('提交失败',e);
  }finally{
	  uni.hideLoading()
  }

}
  }
}
</script>

<style>
.container {
  padding: 20rpx 20rpx;
  background-color: #f5f5f5;

}

.page-header {
  margin-bottom: 40rpx;
}


.header-title {
  font-size: 48rpx;
  font-weight: bold;
  color: #333;
  text-align: center;
  padding: 20rpx 0;
  border-bottom: 2rpx solid #eee;
}

.form-container {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 40rpx 30rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
}

.form-group {
  margin-bottom: 30rpx;
}

.form-label {

	display: flex;
	font-size: 32rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 20rpx;
	padding-bottom: 10rpx;
	border-bottom: 2rpx solid #f0f0f0;
	.section-btn {
		display: inline-block;
		font-weight: 400;
		margin-left: 100rpx;
		padding: 10rpx 30rpx;
		background-color: #f0f0f0;
		color: #666;
		border-radius: 30rpx;
		font-size: 28rpx;
		cursor: pointer;
	
}
}

.form-input {
  position: relative;
}

.form-input input {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  box-sizing: border-box;
  font-size: 32rpx;
  background-color: #f9f9f9;
  transition: border-color 0.3s;
}

.form-input input:focus {
  border-color: #55aaff;
  outline: none;
}

.form-select {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  box-sizing: border-box;
  font-size: 32rpx;
  color: #666;
  background-color: #f9f9f9;
  position: relative;
  transition: border-color 0.3s;
}

.form-select:focus {
  border-color: #55aaff;
  outline: none;
}

.select-arrow {
  width: 32rpx;
  height: 32rpx;
  color: #999;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  padding: 40rpx 30rpx 20rpx;
  background-color: #fff;
  margin-top: 20rpx;
  border-radius: 12rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
}

.btn {
  flex: 1;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 44rpx;
  font-size: 32rpx;
  margin: 0 20rpx;
  font-weight: 500;
  transition: all 0.3s;
}

.cancel {
  background-color: #f5f5f5;
  color: #666;
  border: 1rpx solid #ddd;
}

.cancel:active {
  background-color: #eee;
}

.submit {
  background-color: #55aaff;
  color: #fff;
  border: none;
}

.submit:active {
  background-color: #3e93d6;
}
</style>